<?php
/**
 * Created by openXtiger.org.
 * User: xtiger
 * Date: 2009-6-17
 * Time: 11:12:32
 */
/*public */function xtb_system_cache() {
    return 'header,system_cache,footer';
}
/*public */function xtb_system_cacheaction() {
    if (xt_get('filesubmit')) {
        $filekind =  xt_pget('filekind',array('xtiger','setting','view'));
        if(in_array('xtiger', $filekind)) {
            include MS_XTIGER.'./config/Configuration.php';
            $configuration = ConfigurationManager::getConfiguration()->getRuntimeConfiguration();
            xt_writefile(MS_CACHEPATH.'./RuntimeXConfiguration.cache.php', $configuration, 'php', 'w', 0);                               
        }
        if(in_array('setting', $filekind)) {
            $setting = xt_load('model', 'settingmodel');
            $setting('updatecache');
        }
        if(in_array('view', $filekind)) {
            $cachedir = MS_CACHEPATH.'./tpl';
            $filearr = xt_readdir($cachedir);
            foreach ($filearr as $file) {
                @unlink($cachedir.'/'.$file);
            }
        }
    }
    return xtr_message('cache_delete_success','/system/cache');
}
/*public */function xtb_system_resource($op='') {
    switch($op) {
        case 'details':
            $configAttributes = xt_load('cache','securitySource');
            $pattern =  xt_gget('pattern','');
            $decide =  xt_gget('decide','');
            if(empty($pattern) || empty($decide) || !isset($configAttributes[$decide]) || !isset($configAttributes[$decide][$pattern]))
                return xtr_message('resource_details_error','/system/resource');
            $roles = xt_load('cache','roles');
            xt_set('roles',$roles);
            xt_set('pattern',$pattern);
            xt_set('decide',$decide);
            xt_set('auth',$configAttributes[$decide][$pattern]);
            return 'header,resource_details,footer';
        case 'detailsupdate':
            $roles = xt_pget('roles',array());
            $configAttributes = xt_load('cache','securitySource');
            $pattern =  xt_pget('pattern','');
            $decide =  xt_pget('decide','');
            if(empty($pattern) || empty($decide) || !in_array($decide,array('affirmative','unanimous','consensus')) 
                || !isset($configAttributes[$decide]) || !isset($configAttributes[$decide][$pattern]))
                return xtr_message('resource_details_error','/system/resource');
            $r = 0;
            foreach($roles as $v){
                $r |= $v;   
            }
            $configAttributes[$decide][$pattern] = $r;
            xt_save('cache','securitySource',$configAttributes);
            $model = xt_load('model','cachemodel');
            $model('reflashRoles',$configAttributes);
            return xtr_message('resource_update_success','/system/resource');
        case 'update':
            $dels = xt_pget('delresource',array());
            $pattern =  xt_pget('pattern',array());
            $decide =  xt_pget('decide',array());
            $role =  xt_pget('role',array());
            $configAttributes = array('allowIfAbstainDecision'=>xt_pget('abstain',0));
            foreach($pattern as $id => $v) {
                if(in_array($id,$dels)){
                    continue;
                }
                if(empty($pattern[$id]) || empty($decide[$id]) || $role[$id]==='' ) continue;
                if(!isset($configAttributes[$decide[$id]])){
                    $configAttributes[$decide[$id]] = array($pattern[$id]=>$role[$id]);
                    continue;
                }
                $x = isset($configAttributes[$decide[$id]][$pattern[$id]])?$configAttributes[$decide[$id]][$pattern[$id]]:0; 
                $configAttributes[$decide[$id]][$pattern[$id]] = $x | $role[$id];
            }
            xt_save('cache','securitySource',$configAttributes);
            return xtr_message('resource_update_success','/system/resource');
    }

    $configAttributes = xt_load('cache','securitySource');
    $roles = xt_load('cache','roles');
    $resources = array();
    $allow = 0;
    foreach($configAttributes as $key=>$v) {
        if($key=='allowIfAbstainDecision') {
            $allow = intval($v);
            continue;
        }
        foreach($v as $k1=>$v1) {
            $rolenames=array();
            foreach($roles as $r) {
                if((intval($r[0]) & $v1)>0 ) {$rolenames[] =$r[1];}
            }
            $resources[] = array($key,$k1,$v1,implode($rolenames,' | '));
        }

    }
    xt_set('resources',$resources);
    xt_set('abstain',$allow);
    return 'header,resource,footer';
}

/*public */function xtb_system_role($op='') {
    $roles = xt_load('cache','roles');
    switch($op){
        case 'update':
            $action =  xt_pget('action',1);
            if($action==2){
                $model = xt_load('model','cachemodel');
                $model('reflashRoles');
                return xtr_message('role_update_success','/system/role');  
            }
            $roles1 = array();
            $auth = 0;
            $dels = xt_pget('delrole',array());
            $name =  xt_pget('name',array());
            foreach($roles as $k => $v) {
                $auth =max($roles[$k][0],$auth);
                if(in_array($k,$dels) && $v[2]==0) {
                    continue;
                }
                if(empty($name[$k])) continue;
                $roles1[$k] = $roles[$k];
                $roles1[$k][1] = $name[$k];
            }
            $namenew =  xt_pget('namenew','');
            if(!empty($namenew)) {
                $auth *=2;
                $roles1['ox'.dechex($auth)] = array($auth,$namenew,0,0);
            }
            xt_save('cache','roles',$roles1);
            return xtr_message('role_update_success','/system/role');
    }
    xt_set('roles',$roles);
    return 'header,role,footer';   
}
/*public */function xtb_system_users() {
    $perpage = intval(xt_gget('perpage',20));
    $page = intval(xt_gget('page',1));
	//if(!in_array($perpage, array(20,50,100))) $perpage = 20;
	
    $m = xt_load('model','passportmodel');
    $membersPageSpport = $m('members',$page, $perpage);//
    $roles = xt_load('cache','roles');
    $data = NULL;
    if($list = xt_hashmap($membersPageSpport,'data')) {
        $data = array();
        foreach($list as $v) {
            $rolenames = array();
            foreach($roles as $r) {
                if((intval($r[0]) & $v['roles'])>0 ) {$rolenames[] =$r[1];}
            }
            $v['rolelist'] =  implode($rolenames,' | ');
            $v['lastlogintime'] =  $v['lastlogintime']>0?date('Y-m-d', $v['lastlogintime']):'';
            $data[] = $v;
        }
    }
    xt_set('data',$data);
    xt_set('pagectrl',xt_hashmap($membersPageSpport,'pagectrl'));
    xt_set('pageurl',$membersPageSpport['pageurl'].'&perpage='.$perpage);
    return 'header,users,footer';
}
/*public */function xtb_system_managers($uid=0) {
    $roles = xt_load('cache','roles');
    $auth = 0;
    $username = '';
    if(!empty($uid)) {
        $pm = xt_load('model','passportmodel');
        $rs = $pm('findManagerByUserId',$uid);
        if($rs) {
            $auth = $rs['roles'];
            $username = $rs['username'];         
        } else {
            $uid = 0;
        }
    }
    
    xt_set('uid',$uid);
    xt_set('username',$username);
    xt_set('auth',$auth);
    xt_set('roles',$roles);
    $pm = xt_load('model','passportmodel');

    xt_set('mangers',$pm('managers'));
    return 'header,managers,footer';
}
/*public */function xtb_system_manageraction() {
    if(xt_pget('updatesubmit')){
        $pm = xt_load('model','passportmodel');
        $uid = xt_pget('uid',0);
        $roles = xt_pget('roles',array());
        $rv = 0;
        foreach($roles as $v){
            $rv |= $v;
        }
        if($uid>0) { //update
            if(!($r = $pm('findManagerByUserId',$uid))){
                 return xtr_message('manager_user_not_exist');
            }
            $pm('updateManager',$r['uid'],$rv);
            return xtr_message('manager_update_success','/system/managers');
        }
        $username = xt_pget('username');
        if(empty($username) || !($r = $pm('findMemberByUserName',$username)))
            return xtr_message('manager_user_not_exist');
        $pm('repalceManager',$r['uid'],$username,$rv);
        return xtr_message('manager_update_success','/system/managers');
    }
    if(xt_pget('listsubmit')){
        $del = xt_pget('delete',array());
        if(count($del)>0){
            $pm = xt_load('model','passportmodel');
            $pm('deleteManagers',implode($del,','));
        }
        return xtr_message('manager_update_success','/system/managers');
    }
}
function xtb_system_insert() {
    $pm = xt_load('model','databasemodel');
    $pm('insert');
    echo 'ok';
    return NONE;
}
?>